package artibella.media.controls.ui 
{
	import artibella.IDisposable;
	import artibella.ui.loaders.ProgressBar;
	import artibella.media.controls.AController;
	import artibella.media.controls.AudioController;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class ProgressionTool extends MovieClip implements IDisposable
	{
		private var _controller:AController;
		
		private var _load_bar:ProgressBar;
		private var _prog_bar:ProgressBar;
		
		private var _hz:Sprite;
		
		private var _size:int;
		private var _clr:int;
		
		public function ProgressionTool( ctr:AController, size:int = 100, c:int = 0x000000 ) 
		{
			_controller = ctr;
			_size = size;
			_clr = c;
			
			_init();
		}
		
		
		public function set size( s:int ):void
		{
			if ( _hz != null ) {
				_hz.removeEventListener( MouseEvent.MOUSE_DOWN, _doSeek );
				_hz.removeEventListener( Event.ENTER_FRAME, _slideSeek );
				removeChild( _hz );
				_hz = null;
			}
			
			_size = s;
			
			_load_bar.size = s;
			_prog_bar.size = s;
			
			_init();
			
			_load_bar.scaleX = 1;
		}
		
		private function _init():void
		{
			graphics.clear();		

			graphics.lineStyle( 1, _clr, .3 );
			graphics.moveTo( 0, 0 );
			graphics.lineTo( _size, 0 );
			
			graphics.lineStyle( 1, _clr );
			graphics.moveTo( 0, -3 );
			graphics.lineTo( 0, 4 );
			graphics.moveTo( _size + 1, -3 );
			graphics.lineTo( _size + 1, 4 );

			_drawLoadBar();
			
			_drawProgBar();
			
			if( _hz == null ) _hz = new Sprite();
			_hz.graphics.clear();
			_hz.graphics.beginFill( _clr, 0 );
			_hz.graphics.drawRect( 0, -4, _size, 8 );
			_hz.graphics.endFill();
			_hz.x = 1;
			_hz.y = 0;
			addChild( _hz );
			
			_hz.buttonMode = false;
			_hz.addEventListener( MouseEvent.MOUSE_DOWN, _doSeek );
		}
		
		private function _drawLoadBar():void
		{
			if ( _load_bar != null ) {
				_load_bar.scaleX = 1;
				_load_bar.size = _size;
			}
			else _load_bar = new ProgressBar( _size, 1, _clr );
			_load_bar.x = 1;
			_load_bar.y = 0;
			addChild( _load_bar );
		}
		
		private function _drawProgBar():void
		{
			if ( _prog_bar != null ) {
				_prog_bar.scaleX = 1;
				_prog_bar.size = _size;
			}
			else _prog_bar = new ProgressBar( _size, 3, _clr );
			_prog_bar.x = 1;
			_prog_bar.y = -1;
			addChild( _prog_bar );
		}
		
		// - - -
		
		private function _doSeek( e:MouseEvent ):void
		{
			_hz.removeEventListener( MouseEvent.MOUSE_DOWN, _doSeek );
			
			_hz.addEventListener( Event.ENTER_FRAME, _slideSeek );
			stage.addEventListener( MouseEvent.MOUSE_UP, _doSeekDone );
			
			_controller.setPauseState();
		}
		
		private function _doSeekDone( e:MouseEvent ):void
		{
			stage.removeEventListener( MouseEvent.MOUSE_UP, _doSeekDone );
			
			_hz.removeEventListener( Event.ENTER_FRAME, _slideSeek );
			_hz.addEventListener( MouseEvent.MOUSE_DOWN, _doSeek );
			
			_controller.setSeek( _prog_bar.scaleX );
		}
		
		private function _slideSeek( e:Event ):void
		{
			var position:int;
			
			if ( _hz.mouseX < 0 ) {
				position = 0;
			} else if ( _hz.mouseX > _hz.width * _load_bar.scaleX ){
				position = _hz.width * _load_bar.scaleX;
			} else {
				position = _hz.mouseX;
			}
			
			var percent:Number = position / _hz.width;
			
			_prog_bar.update( percent );
			
			_controller.setTime( percent );
		}
		
		// - - -
		
		public function loadUpdate( v:Number ):void
		{
			if( _load_bar != null ) _load_bar.update( v );
		}
		
		public function progUpdate( v:Number ):void
		{
			if( _prog_bar != null ) _prog_bar.update( v );
		}
		
		// - - -
		
		public function dispose():void
		{
			_load_bar = null;
			
			_prog_bar = null;
			
			_controller = null;
		}
	}
}